/* ==============================================================================
 * 创 建 者：LYT
 * 创建日期：2018-03-14	
 * 功能描述：

 * 修 改 人:
 * 修改时间:
 * 修改描述：
 * ==============================================================================*/

//商品
var getGoodsDetailUrl = baseUrl + 'MallUF/GetGoodsInfo';
//加入购物车
var addCarGoodsUrl = baseUrl + 'MallF/AddBuyCar';
//直接购买
var OnlyBuyUrl = baseUrl + 'MallF/OnlyBuy';

var oid = getURLParameter("oid");

var vu;
var gallery;


$(function() {
	initVue();
	Vue.nextTick(function(){
		bindEvent();
		muiInit();
	})
	
})

function initVue(){
	vu=new Vue({
		el:'#main',
		data:{
			searchVal:'', //搜索内容
			num : 1,
			good : '',
			bannerList : [],
			chooseGoodInfo:[],
			Description : '',//描述
			SpecificationMode : [],//规格
			
		},
		mounted: function() {
			this.getGoodsDetail();
		},
		methods:{
			addCarGoods : addCarGoods,
			getGoodsDetail : getGoodsDetail,
			toBuy : toBuy,
			search:function(){
				if(this.searchVal!=''){
					location.href='searchList.html?goodName='+this.searchVal;
				}else{
					mui.toast('请输入搜索关键词');
				}
			},
		}
	})
}

function toBuy(){
	var GuiGe = '';
	if(vu.SpecificationMode.length){
		if(!$(".sizeBox .mui-active").length){
			mui.toast("请选择规格！");
			return;
		}
		$(".sizeBox .mui-active").each(function(){
			GuiGe += $(this).text() + ','
		})
		GuiGe = GuiGe.substring(0,GuiGe.length-1)
	}
	common.post(OnlyBuyUrl,{ GoodsOID: oid, Count: mui('.mui-numbox').numbox().getValue(), GuiGe : GuiGe},'json',function(r){
		if(r.ErrorCode==0){
			location.href = 'makeSureOrder.html';
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function addCarGoods(){
	var GuiGe = '';
	if(vu.SpecificationMode.length){
		if(!$(".sizeBox .mui-active").length){
			mui.toast("请选择规格！");
			return;
		}
		$(".sizeBox .mui-active").each(function(){
			GuiGe += $(this).text() + ','
		})
		GuiGe = GuiGe.substring(0,GuiGe.length-1)
	}
	var num = mui('.mui-numbox').numbox().getValue();
	common.post(addCarGoodsUrl,{ GoodsOID: oid, Count: num, GuiGe : GuiGe},'json',function(r){
		if(r.ErrorCode==0){
			mui.toast(r.ErrorMsg);
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function getGoodsDetail(){
	common.get(getGoodsDetailUrl,{OID : oid},'json',function(r){
		if(r.ErrorCode==0){
			vu.good = r.Data.GoodsInfo.goods;
			vu.SpecificationMode = r.Data.GoodsInfo.SpecificationMode;
			vu.Description = r.Data.GoodsInfo.Description;
			vu.bannerList = r.Data.GoodsInfo.goods.ImgList.split(',');
			
			Vue.nextTick(function(){
				muiInit();
			})
		}else{
			mui.toast(r.ErrorMsg);
		}
	})
}

function muiInit(){
	mui.init();
	mui('.mui-numbox').numbox().setValue(1);
	gallery = mui('#slider');
	gallery.slider({
		interval: 0 //自动轮播周期，若为0则不自动播放，默认为0；
	});
}

function bindEvent(){
	$("nav").on("tap", "a", function() { //点击触发  
		location.href = $(this).attr("href");
	})
	/*$(".gld_foot_list").on('tap',"a",function(){
		$(this).trigger();
	});*/
	$(".mui-collapse-content").on("tap",".sizeBtn",function(){
		if(!$(this).hasClass("mui-active")){
			$(this).addClass("mui-active").siblings().removeClass("mui-active");
		}
	})
}
